<?php

declare(strict_types=1);
/**
 * 创建人 : Forpastime
 *
 * @link     http://hypercmf.com
 * @document https://hyperf.wiki
 * @contact  forpastime@qq.com
 * @git  https://gitee.com/hypercmf/hypercmf1.0.git
 */
namespace App\Controller\Admin;

use App\Model\SystemSetting;
use App\Utils\Result;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\Redis\Redis;
use Hyperf\Utils\ApplicationContext;
use App\Middleware\Auth\AdminTokenMiddleware;
use App\Middleware\Auth\AdminPowerMiddleware;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\Middlewares;
use App\Annotation\AdminNode;


/**
 * @AdminNode(name="系统设置",isMenu=1,sort=10,style="layui-icon-set")
 * @Middlewares({
 *      @Middleware(AdminTokenMiddleware::class),
 *      @Middleware(AdminPowerMiddleware::class)
 * })
 * @AutoController(prefix="admin/system")
 */
class SystemController
{
    /**
     * @var SystemSetting
     * @Inject()
     */
    protected $system;

    /**
     * @var RequestInterface
     * @Inject()
     */
    protected $request;

    public function index()
    {

    }

    /**
     * @AdminNode(name="存储设置",isMenu=1)
     *
     * @return mixed
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    public function storage()
    {
        $params = $this->request->all();
        if($this->request->isMethod('post')){
            $info['name'] = $params['name'];
            $info['key'] = $params['key'];
            $info['value'] = json_encode($params);
            if(isset($params['id'])){
                $res = $this->system->where('id',$params['id'])->update($info);
            }else{
                $res = $this->system->insertGetId($info);
            }
            if($res){
                $redis = ApplicationContext::getContainer()->get(Redis::class);
                $redis->del('storage_setting');
                return Result::success();
            }else{
                return Result::error('操作失败');
            }
        }else{
            $info = $this->system->where('key',$params['key'])->first();
            if(!empty($info)){
                $res = $info->toArray();
                $res['value'] = json_decode($res['value']);

                $res['aliyun']['protocol'] = env('OSS_PROTOCOL');
                $res['aliyun']['access_id'] = env('OSS_ACCESS_ID');
                $res['aliyun']['access_secret'] = env('OSS_ACCESS_SECRET');
                $res['aliyun']['bucket'] = env('OSS_BUCKET');
                $res['aliyun']['endpoint'] = env('OSS_ENDPOINT');
                $res['aliyun']['style'] = env('OSS_STYLE');

                $res['qiniu']['protocol'] = env('QINIU_PROTOCOL');
                $res['qiniu']['access_id'] = env('QINIU_ACCESS_ID');
                $res['qiniu']['access_secret'] = env('QINIU_ACCESS_SECRET');
                $res['qiniu']['bucket'] = env('QINIU_BUCKET');
                $res['qiniu']['endpoint'] = env('QINIU_DOMAIN');
                $res['qiniu']['style'] = env('QINIU_STYLE');

                return Result::success($res);
            }else{
                $res['aliyun']['protocol'] = env('OSS_PROTOCOL');
                $res['aliyun']['access_id'] = env('OSS_ACCESS_ID');
                $res['aliyun']['access_secret'] = env('OSS_ACCESS_SECRET');
                $res['aliyun']['bucket'] = env('OSS_BUCKET');
                $res['aliyun']['endpoint'] = env('OSS_ENDPOINT');
                $res['aliyun']['style'] = env('OSS_STYLE');

                $res['qiniu']['protocol'] = env('QINIU_PROTOCOL');
                $res['qiniu']['access_id'] = env('QINIU_ACCESS_ID');
                $res['qiniu']['access_secret'] = env('QINIU_ACCESS_SECRET');
                $res['qiniu']['bucket'] = env('QINIU_BUCKET');
                $res['qiniu']['endpoint'] = env('QINIU_DOMAIN');
                $res['qiniu']['style'] = env('QINIU_STYLE');
                return Result::success($res);
            }
        }
    }
}
